DZApp.service(
				'orderPayService',
				function(commonService) {
					return {
						query : function(postData) {
							var url = "/operationService/queryOrderPay";
							return commonService
									.commonAjaxByPost(url, postData);
						},
                        getCheckCode : function(postData) {
							var url = "/operationService/getCheckCode";
							return commonService
									.commonAjaxByPost(url, postData);
						},
                        listMerchantByManagerId : function(postData) {
                            var url = "/operationService/listMerchant";
                            return commonService
                                .commonAjaxByPost(url, postData);
                        },
                        pay : function(postData) {
                            var url = "/operationService/pay";
                            return commonService
                                .commonAjaxByPost(url, postData);
                        },
						validate : function($scope) {
							return true;
						}
					};
				});
DZApp.controller('orderPayController', function($scope, orderPayService,
		$state) {
	$scope.orderInfo = {};
	$scope.orderItemlist = {};
	$scope.queryMainSeviceSelect = {};
    $scope.orderSummaryModel = {};
	$scope.query = {};
	$scope.checkBoxs = {};
	$scope.aValue = '';
	$scope.checkCode = '';
	$scope.isNeedConditionValidate = false;
    $scope.openTitle = '转账';
	var pagation ={
			prePageNum:20,
			curPageNo:1,
			curPageNo:1,
			param:null
	};
	// 翻页出发分析
	$scope.onPageChange = function() {
		pagation.curPageNo = $scope.currentPage;
		queryPagination($.param(pagation));
	};
	
	// 分页回调函数
	var doResponseCallback = function(data) {
		if ("success" == data.retCode) {
			var data  = eval(data.bizData);
			$scope.query = data.list;
			$scope.pageCount = data.totalPage;
		} else {
			bootboxWindow.alert('警告查询错误:' + data.errorMsg, null);
		}
	};
	// 參數封裝
	var doParam = function(){
		pagation.param = JSON.stringify($scope.orderSummaryModel);
		return $.param(pagation);
	}
	// 分页查询
	var queryPagination = function(param){
		$scope.query = null;
	//	$('#table_1').DataTable().destroy();
        orderPayService.query(param).then(doResponseCallback);
	}
	
	// 是否初始化的就查询
	var isQueryInit = true;
	if (isQueryInit) {
		queryPagination(doParam());
	}
    orderPayService.listMerchantByManagerId().then(function(data) {
        $scope.listMerchantByManagerId = eval(data.bizData);
    });
	// 查詢
	$scope.queryOrderInfo = function() {
		$scope.query = null;
		queryPagination(doParam());
	}
	// 查詢
	$scope.pay = function() {

        $scope .checkBoxs={};
        var arr=[];
        $('input[name="merchantCodeBox"]:checked').each(function(){
          for (var i=0;i<  $scope.query.length;i++){

          	if($(this).val()==$scope.query[i].merchantCode){
                arr.push($scope.query[i]);
			}
		  }

        });
        $scope.checkBoxs=arr;
	}
    $scope.editData = function() {
        var postData = $.param({
            orderAlreadyPaid : JSON.stringify($scope.checkBoxs),
            checkCode : $("#checkCode").val()
        });
        orderPayService.pay(postData).then(function(data) {
            if (data.retCode == 'success') {
                bootboxWindow.alert('支付成功请等待支付宝处理结果');
                $state.reload();
            } else {
                bootboxWindow.alert('部分支付失败请稍后再试，' + data.errorMsg, null);
                $state.reload();
            }
        });
    };
    $scope.getCheckCode = function() {
        var postData = $.param({
            orderAlreadyPaid : JSON.stringify($scope.checkBoxs)
        });
        orderPayService.getCheckCode(postData).then(function(data) {
            if (data.retCode == 'success') {
                bootboxWindow.alert('获取成功');
            } else {
                bootboxWindow.alert('获取失败，' + data.errorMsg, null);
            }
        });
    };
});
